package com.mk.legoscans2.data;

import java.net.MalformedURLException;
import java.net.URL;

import com.mk.legoscans2.Utils;

import android.graphics.Bitmap;

public class HttpThumbnailService implements ThumbnailService {

	private ErrorReoportingSerivice _errorReoportingSerivice;
	
	private static double MAX_SIZE = 130.0;

	public HttpThumbnailService(ErrorReoportingSerivice errorReoportingSerivice)
	{
		_errorReoportingSerivice = errorReoportingSerivice;
	}
	
	public Bitmap getThumbnail(String scanId) {
		try {
			Bitmap bmp = Utils.getImageBitmapFromUrl(new URL(Utils.getThumbnailUrl(scanId)));
			if(bmp == null)
			{
				_errorReoportingSerivice.reportBrokenThumbnail(scanId);
				return null;
			}
			
			double scale = MAX_SIZE / bmp.getWidth();
			int width = (int) (bmp.getWidth() * scale);
			int height = (int) (bmp.getHeight() * scale);
			
			return Bitmap.createScaledBitmap(bmp, width, height, false);
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
	}

}
